package com.panicstyle.Moojigae;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.regex.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.*;
import org.apache.http.client.CookieStore;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.ClientContext;
import org.apache.http.cookie.Cookie;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.ContentBody;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;

import com.panicstyle.Moojigae.HttpRequest;
import com.panicstyle.Moojigae.ItemsActivity;
import com.panicstyle.Moojigae.RecentItemsActivity;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class BoardActivity extends ListActivity {
	TextView selection;
//	String[] items;
	
	protected String boardTitle;
	protected String boardCode;
	protected HttpClient httpClient;
	protected HttpContext httpContext;
    List<HashMap<String, String>> arrayItems;

    private static class EfficientAdapter extends BaseAdapter {
        private LayoutInflater mInflater;
        private Bitmap mIcon1;
        private Bitmap mIcon2;
        private List<HashMap<String,String>> arrayItems;

        public EfficientAdapter(Context context, List<HashMap<String, String>> data) {
            // Cache the LayoutInflate to avoid asking for a new one each time.
            mInflater = LayoutInflater.from(context);

            // Icons bound to the rows.
            mIcon1 = BitmapFactory.decodeResource(context.getResources(), R.drawable.icon_new);
            mIcon2 = BitmapFactory.decodeResource(context.getResources(), R.drawable.icon_none);
            
            arrayItems = data;
        }

        /**
         * The number of items in the list is determined by the number of speeches
         * in our array.
         *
         * @see android.widget.ListAdapter#getCount()
         */
        public int getCount() {
            return arrayItems.size() ;
        }

        /**
         * Since the data comes from an array, just returning the index is
         * sufficent to get at the data. If we were using a more complex data
         * structure, we would return whatever object represents one row in the
         * list.
         *
         * @see android.widget.ListAdapter#getItem(int)
         */
        public Object getItem(int position) {
            return position;
        }

        /**
         * Use the array index as a unique id.
         *
         * @see android.widget.ListAdapter#getItemId(int)
         */
        public long getItemId(int position) {
            return position;
        }

        /**	
         * Make a view to hold each row.
         *
         * @see android.widget.ListAdapter#getView(int, android.view.View,
         *      android.view.ViewGroup)
         */
        public View getView(int position, View convertView, ViewGroup parent) {
            // A ViewHolder keeps references to children views to avoid unneccessary calls
            // to findViewById() on each row.
            ViewHolder holder;

            // When convertView is not null, we can reuse it directly, there is no need
            // to reinflate it. We only inflate a new View when the convertView supplied
            // by ListView is null.
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.list_item_boardview, null);

                // Creates a ViewHolder and store references to the two children views
                // we want to bind data to.
                holder = new ViewHolder();
                holder.title = (TextView) convertView.findViewById(R.id.title);
                holder.icon = (ImageView) convertView.findViewById(R.id.icon);

                convertView.setTag(holder);
            } else {
                // Get the ViewHolder back to get fast access to the TextView
                // and the ImageView.
                holder = (ViewHolder) convertView.getTag();
            }
            HashMap<String, String> item = new HashMap<String, String>();
            String title = null;
            String isNew = null;
            item = (HashMap<String, String>)arrayItems.get(position);
            title = (String)item.get("title");
            isNew = (String)item.get("isNew");
            // Bind the data efficiently with the holder.
            holder.title.setText(title);
            if (isNew == "1") {
            	holder.icon.setImageBitmap(mIcon1);
            } else {
            	holder.icon.setImageBitmap(mIcon2);
            }

            return convertView; 
        }
        
        static class ViewHolder {
            TextView title;
            ImageView icon;
        }
    }
	
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.main);
        
        MoojigaeApplication app = (MoojigaeApplication)getApplication();
        httpClient = app.httpClient;
        httpContext = app.httpContext;

        intenter();

/*        
        // Login
		Login login = new Login();
		
		if (!login.LoginTo(httpClient, httpContext, cookieStore)) {
			return;
		}
*/
		
		arrayItems = getData(httpClient, httpContext);

        setListAdapter(new EfficientAdapter(this, arrayItems));
    }
    
    public void intenter() {
//    	Intent intent = getIntent();  // 값을 가져오는 인텐트 객체생성
    	Bundle extras = getIntent().getExtras();
    	// 가져온 값을 set해주는 부분
    	
    	boardTitle = extras.getString("BOARD_TITLE").toString();
    	boardCode = extras.getString("BOARD_CODE").toString();
    }
    
    protected List<HashMap<String,String>> getData(HttpClient httpClient, HttpContext httpContext) {		

        HashMap<String, String> item;
        List<HashMap<String, String>> arrayItems = new ArrayList<HashMap<String, String>>();
        
        if (boardCode.equalsIgnoreCase("maul")) {
	        item = new HashMap<String, String>();
	        item.put("link",  "recent");
	        item.put("title",  "최근글보기");
	        arrayItems.add( item );
	        
	        item = new HashMap<String, String>();
	        item.put("link",  "-");
	        item.put("title",  "이야기방");
	        arrayItems.add( item );
	        
	        item = new HashMap<String, String>();
	        item.put("link",  "mvTopic");
	        item.put("title",  " > 이야기방");
	        arrayItems.add( item );
	        
	        item = new HashMap<String, String>();
	        item.put("link",  "mvEduBasicRight");
	        item.put("title", " > 교육기본권");
	        arrayItems.add( item );
	        
	        item = new HashMap<String, String>();
	        item.put("link",  "mvTopic10Year");
	        item.put("title",  " > 10주년 행사");
	        arrayItems.add( item );
	        
	        item = new HashMap<String, String>();
	        item.put("link",  "mvTopicGoBackHome");
	        item.put("title",  " > 무지개 촌(村)");
	        arrayItems.add( item );
	        
	        item = new HashMap<String, String>();
	        item.put("link",  "mvGongi");
	        item.put("title",  "마을 공지사항");
	        arrayItems.add( item );
	        
	        item = new HashMap<String, String>();
	        item.put("link",  "-");
	        item.put("title",  "공동체사업부");
	        arrayItems.add( item );
	        
	        item = new HashMap<String, String>();
	        item.put("link", "mvGongDong");
	        item.put("title", " > 공동체사업부");
	        arrayItems.add( item );
	        
	        item = new HashMap<String, String>();
	        item.put("link", "mvGongDongFacility");
	        item.put("title", " > 시설기획팀");
	        arrayItems.add( item );
	        
	        item = new HashMap<String, String>();
	        item.put("link", "mvGongDongEvent");
	        item.put("title", " > 행사기획팀");
	        arrayItems.add( item );
	        
	        item = new HashMap<String, String>();
	        item.put("link", "mvGongDongLocalcommunity");
	        item.put("title", " > 지역사업팀");
	        arrayItems.add( item );
	        
	        item = new HashMap<String, String>();
	        item.put("link", "mvDonghowhe");
	        item.put("title", "마을 동아리방");
	        arrayItems.add( item );
	        
	        item = new HashMap<String, String>();
	        item.put("link", "-");
	        item.put("title", "어울림품앗이");
	        arrayItems.add( item );
	        
	        item = new HashMap<String, String>();
	        item.put("link", "mvPoomASee");
	        item.put("title", " > 어울림품앗이");
	        arrayItems.add( item );
	        
	        item = new HashMap<String, String>();
	        item.put("link", "mvPoomASeeWantBiz");
	        item.put("title", " > 거래하고싶어요");
	        arrayItems.add( item );
	        
	        item = new HashMap<String, String>();
	        item.put("link", "mvPoomASeeBized");
	        item.put("title", " > 거래했어요");
	        arrayItems.add( item );
	        
	        item = new HashMap<String, String>();
	        item.put("link", "-");
	        item.put("title", "교육사업부");
	        arrayItems.add( item );
	        
	        item = new HashMap<String, String>();
	        item.put("link", "mvEduLove");
	        item.put("title", " > 교육사랑방");
	        arrayItems.add( item );
	        
	        item = new HashMap<String, String>();
	        item.put("link", "mvEduVillageSchool");
	        item.put("title", " > 마을학교");
	        arrayItems.add( item );
	        
	        item = new HashMap<String, String>();
	        item.put("link", "mvEduDream");
	        item.put("title", " > 또하나의꿈");
	        arrayItems.add( item );
	        
	        item = new HashMap<String, String>();
	        item.put("link", "mvEduSpring");
	        item.put("title", " > 교육샘");
	        arrayItems.add( item );
	        
	        item = new HashMap<String, String>();
	        item.put("link", "mvEduSpring");
	        item.put("title", " > 만두");
	        arrayItems.add( item );
	        
	        item = new HashMap<String, String>();
	        item.put("link", "mvMarketBoard");
	        item.put("title", "무지개장터");
	        arrayItems.add( item );
	        
	        item = new HashMap<String, String>();
	        item.put("link", "-");
	        item.put("title", "무지개지평선");
	        arrayItems.add( item );
	        
	        item = new HashMap<String, String>();
	        item.put("link", "mvHorizonIntroduction");
	        item.put("title", " > 가족소개");
	        arrayItems.add( item );
	        
	        item = new HashMap<String, String>();
	        item.put("link", "mvHorizonLivingStory");
	        item.put("title", " > 사는 얘기");
	        arrayItems.add( item );
	        
	        item = new HashMap<String, String>();
	        item.put("link", "-");
	        item.put("title", "사무국");
	        arrayItems.add( item );
	        
	        item = new HashMap<String, String>();
	        item.put("link", " > 마을주민 연락처");
	        item.put("title", "mvSecretariatAddress");
	        arrayItems.add( item );
	        
	        item = new HashMap<String, String>();
	        item.put("link", "mvSecretariatOldData");
	        item.put("title", " > 마을 자료실");
	        arrayItems.add( item );
	        
	        item = new HashMap<String, String>();
	        item.put("link", "mvMinutes");
	        item.put("title", "회의록방");
	        arrayItems.add( item );
	        
	        item = new HashMap<String, String>();
	        item.put("link", "mvDirectors");
	        item.put("title", "이사회");
	        arrayItems.add( item );
	        
	        item = new HashMap<String, String>();
	        item.put("link", "mvCommittee");
	        item.put("title", "운영위");
	        arrayItems.add( item );
	        
	        item = new HashMap<String, String>();
	        item.put("link", "mvHongbo");
	        item.put("title", "홍보위");
	        arrayItems.add( item );
	        
        } else if (boardCode.equalsIgnoreCase("school1")) {
	        item = new HashMap<String, String>();
	        item.put("link", "recent");
	        item.put("title", "최근글보기");
	        arrayItems.add( item );
	        
	        item = new HashMap<String, String>();
	        item.put("link", "mjGongi");
	        item.put("title", "초등 공지사항");
	        arrayItems.add( item );
	        
	        item = new HashMap<String, String>();
	        item.put("link", "mjFreeBoard");
	        item.put("title", "자유게시판");
	        arrayItems.add( item );
	        
	        item = new HashMap<String, String>();
	        item.put("link", "mjTeacher");
	        item.put("title", "교사방");
	        arrayItems.add( item );
	        
	        item = new HashMap<String, String>();
	        item.put("link", "mjTeachingData");
	        item.put("title", "교사자료실");
	        arrayItems.add( item );
	        
	        item = new HashMap<String, String>();
	        item.put("link", "mjJunior");
	        item.put("title", "아이들방");
	        arrayItems.add( item );
	        
	        item = new HashMap<String, String>();
	        item.put("link", "mjParent");
	        item.put("title", "학부모방");
	        arrayItems.add( item );
	        
	        item = new HashMap<String, String>();
	        item.put("link", "mjParentMinutes");
	        item.put("title", "학부모 회의록");
	        arrayItems.add( item );
	        
	        item = new HashMap<String, String>();
	        item.put("link", "mjAmaDiary");
	        item.put("title", "품앗이분과");
	        arrayItems.add( item );
	        
	        item = new HashMap<String, String>();
	        item.put("link", "mjPhoto");
	        item.put("title", "사진첩&동영상");
	        arrayItems.add( item );
	        
	        item = new HashMap<String, String>();
	        item.put("link", "mjData");
	        item.put("title", "학교 자료실");
	        arrayItems.add( item );
	        
        } else {
	        item = new HashMap<String, String>();
	        item.put("link", "recent");
	        item.put("title", "최근글보기");
	        arrayItems.add( item );
	        
	        item = new HashMap<String, String>();
	        item.put("link", "msGongi");
	        item.put("title", "중등 공지사항");
	        arrayItems.add( item );
	        
	        item = new HashMap<String, String>();
	        item.put("link", "msFreeBoard");
	        item.put("title", "학교이야기방");
	        arrayItems.add( item );
	        
	        item = new HashMap<String, String>();
	        item.put("link", "msFreeComment");
	        item.put("title", "자유게시판");
	        arrayItems.add( item );
	        
	        item = new HashMap<String, String>();
	        item.put("link", "msTeacher");
	        item.put("title", "교사방");
	        arrayItems.add( item );
	        
	        item = new HashMap<String, String>();
	        item.put("link", "msSenior");
	        item.put("title", "숙제방");
	        arrayItems.add( item );
	        
	        item = new HashMap<String, String>();
	        item.put("link", "msStudent");
	        item.put("title", "아이들방");
	        arrayItems.add( item );
	        
	        item = new HashMap<String, String>();
	        item.put("link", "msStudentAssociation");
	        item.put("title", "학생회방");
	        arrayItems.add( item );
	        
	        item = new HashMap<String, String>();
	        item.put("link", "msParent");
	        item.put("title", "학부모방");
	        arrayItems.add( item );
	        
	        item = new HashMap<String, String>();
	        item.put("link", "msPhoto");
	        item.put("title", "사진첩&동영상");
	        arrayItems.add( item );
	        
	        item = new HashMap<String, String>();
	        item.put("link", "msData");
	        item.put("title", "학교자료실");
	        arrayItems.add( item );
        	
        }
        
        return arrayItems;
    }

    public void onListItemClick(ListView parent, View v, int position, long id) {
        HashMap<String, String> item = new HashMap<String, String>();
        String title = null;
        String link = null;
        item = (HashMap<String, String>)arrayItems.get(position);
        title = (String)item.get("title");
        link = (String)item.get("link");

        if (link.equalsIgnoreCase("-")) return;
        
        if (link.equalsIgnoreCase("recent")) {
        	Intent intent = new Intent(this, RecentItemsActivity.class);
	        intent.putExtra("ITEMS_TITLE", boardTitle);
	        intent.putExtra("ITEMS_LINK", boardCode);
	        startActivity(intent);
        } else {
        	Intent intent = new Intent(this, ItemsActivity.class);
	        intent.putExtra("ITEMS_TITLE", title);
	        intent.putExtra("ITEMS_LINK", link);
	        startActivity(intent);
        }
    }
}